//
//  LoanDetailsTitleView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/12.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class LoanDetailsTitleView: UIView {

    init(model:GenericLoansModel) {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.red
        
        let titleLabel = UILabel()
        titleLabel.text = model.organization_name! + "-" + model.product_name!
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        titleLabel.textColor = UIColor.white
        self.addSubview(titleLabel)
        
        let lendersLabel = UILabel()
        lendersLabel.text = model.time_advange
        lendersLabel.textAlignment = .center
        lendersLabel.textColor = UIColor.white
        lendersLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        lendersLabel.layer.borderColor = UIColor.white.cgColor
        lendersLabel.layer.borderWidth = 1
        self.addSubview(lendersLabel)
        
        let mortgageLabel = UILabel()
        mortgageLabel.text = model.mortgage
        mortgageLabel.textAlignment = .center
        mortgageLabel.textColor = UIColor.white
        mortgageLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        mortgageLabel.layer.borderWidth = 1
        mortgageLabel.layer.borderColor = UIColor.white.cgColor
        self.addSubview(mortgageLabel)
        
        let typeLabel = UILabel()
        typeLabel.textColor = UIColor.white
        typeLabel.text = model.identitychoose
        typeLabel.textAlignment = .center
        typeLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        typeLabel.layer.borderWidth = 1
        typeLabel.layer.borderColor = UIColor.white.cgColor
        self.addSubview(typeLabel)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 20))
            make.width.lessThanOrEqualToSuperview()
            make.height.greaterThanOrEqualTo(10)
            make.centerX.equalToSuperview()
        }
        
        lendersLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.width.equalTo((lendersLabel.text?.size(font: 16, height: RATIO_H(maxNum: 25)).width)!)
            make.height.equalTo(RATIO_H(maxNum: 25))
            make.right.equalTo(titleLabel.snp.centerX).offset(-2)
        }
        
        mortgageLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(lendersLabel)
            make.left.equalTo(titleLabel.snp.centerX).offset(2)
            make.width.equalTo((mortgageLabel.text?.size(font: 16, height: RATIO_H(maxNum: 25)).width)!)
            make.height.equalTo(lendersLabel)
        }
        
        var width:CGFloat = 0
        let textWidth = typeLabel.text?.size(font: 16, height: RATIO_H(maxNum: 25)).width ?? 0
        if textWidth <= 10 {
            typeLabel.layer.borderWidth = 0
        }else if textWidth > (ScreenW-RATIO(maxNum: 40)) {
            width = ScreenW-RATIO(maxNum: 50)
        }else{
            width = textWidth
        }
        
        
        typeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(mortgageLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
            make.centerX.equalToSuperview()
            make.width.equalTo(width)
            make.height.equalTo(mortgageLabel)
        }
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


